#!/usr/bin  python
# _*_ coding:utf-8 _*_
"""
----------------------------------
    File Name: parent
    Description:
        Author: lhw
        Date: 2020/7/31
-----------------------------------
"""

from django.db import models


# Create your models here.


class BaseModel(models.Model):
    first_name = models.CharField(max_length=255, blank=True, null=True)
    last_name = models.CharField(max_length=255, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    class Meta:
        abstract = True


class Babies(BaseModel):
    full_name = models.CharField(max_length=255, blank=True, null=True)
    birthday = models.DateField(blank=True, null=True)
    expected_birthday = models.DateField(blank=True, null=True)
    gender = models.CharField(max_length=10, blank=True, null=True)
    mrn_hash = models.CharField(max_length=255, blank=True, null=True)
    mrn_last4 = models.CharField(max_length=255, blank=True, null=True)
    discharged = models.BooleanField(default=False, verbose_name="discharged or no")
    discharged_at = models.DateField(blank=True, null=True)
    validated = models.IntegerField(blank=True, null=True)
    babysense_id = models.CharField(max_length=255, blank=True, null=True)
    status = models.CharField(max_length=255, blank=True, null=True, default="In NICU")
    hospital = models.CharField(max_length=60, blank=True, null=True)
    group = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        db_table = 'babies'
        verbose_name = "baby information"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.full_name

    def baby_tojson(self):
        res = {
            "full_name": self.full_name,
            "birthday": self.birthday.strftime("%Y-%m-%d"),
            "expected_birthday": self.expected_birthday.strftime("%Y-%m-%d"),
            "gender": self.gender,
            "mrn_last4": self.mrn_last4,
            "status": self.status
        }
        return res

    @property
    def EMR_Name(self):
        return "{} {}".format(self.first_name, self.last_name)

    @property
    def App_Name(self):
        return self.full_name

    @property
    def MRN(self):
        return "XXXXX{}".format(self.mrn_last4)

    @property
    def created_time(self):
        return self.created_at.strftime("%Y-%m-%d %H:%M:%S")


class Users(BaseModel):
    full_name = models.CharField(max_length=255, blank=True, null=True)
    email = models.EmailField(unique=True, max_length=255)
    password = models.CharField(max_length=255, blank=True, null=True)
    relationship = models.CharField(max_length=255, blank=True, null=True)
    study_id_number = models.CharField(max_length=50, blank=True, null=True)
    stop_service = models.BooleanField(default=True, verbose_name="whether it was service")
    hospital = models.CharField(max_length=60, blank=True, null=True)
    group = models.CharField(max_length=50, blank=True, null=True)
    #  保留原来的字段，可能和其他的业务相关
    spouse_id = models.IntegerField(blank=True, null=True)
    phone = models.CharField(max_length=255, blank=True, null=True)
    security_code = models.IntegerField(blank=True, null=True)
    access_token = models.TextField(blank=True, null=True)
    refresh_token = models.TextField(blank=True, null=True)
    token_expiry_time = models.DateTimeField(blank=True, null=True)
    token_scope = models.CharField(max_length=255, blank=True, null=True)
    file_name = models.CharField(max_length=255, blank=True, null=True)
    recommended_notifications = models.IntegerField(blank=True, null=True)
    post_notifications = models.IntegerField(blank=True, null=True)
    comment_notifications = models.IntegerField(blank=True, null=True)
    daily_update_notifications = models.IntegerField(blank=True, null=True)
    notification_counter = models.IntegerField(blank=True, null=True)
    scheduled = models.IntegerField(blank=True, null=True)
    scheduled_from = models.TimeField(blank=True, null=True)
    scheduled_to = models.TimeField(blank=True, null=True)
    testers = models.IntegerField(blank=True, null=True)

    class Meta:
        db_table = 'users'
        verbose_name = 'user information'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.full_name

    def user_tojson(self):
        res = {
            "id": self.id,
            "email": self.email,
            "full_name": self.full_name,
            "relationship": self.relationship,
            "phone": self.phone,
            "hospital": self.hospital,
            "file_name": self.file_name,
        }
        return res

    def is_parent(self):
        return self.relationship.lower() == 'father' or self.relationship.lower() == 'mother'


class UsersBabies(models.Model):
    users = models.ForeignKey(
        to='Users',
        related_name='users_babies',
        db_constraint=False,
        on_delete=models.CASCADE
    )
    babies = models.ForeignKey(
        to='Babies',
        related_name='users_babies',
        db_constraint=False,
        on_delete=models.CASCADE
    )
    group = models.CharField(max_length=50, blank=True, null=True)
    family_id = models.IntegerField(blank=True, null=True)
    hospital = models.CharField(max_length=60, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    class Meta:
        db_table = 'usersbabies'
        verbose_name = 'user-baby-information'
        verbose_name_plural = verbose_name
        ordering = ['family_id', 'group']
        unique_together = [
            ("users", "babies"),
        ]


class Appointments(models.Model):
    title = models.CharField(max_length=255, blank=True, null=True)
    date = models.DateField(blank=True, null=True)
    time = models.TimeField(blank=True, null=True)
    babies = models.ForeignKey(
        to='Babies',
        db_constraint=False,
        on_delete=models.CASCADE
    )
    hospital = models.CharField(max_length=60, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    class Meta:
        db_table = 'appointments'
        verbose_name = 'appointments information'
        verbose_name_plural = verbose_name
        ordering = ['-date', "-time"]

    def __str__(self):
        return self.title

    @property
    def update_time(self):
        return self.updated_at.strftime("%Y-%m-%d %H:%M:%S")


class Vaccinations(models.Model):
    date = models.DateField(blank=True, null=True)
    vaccinations = models.CharField(max_length=255, blank=True, null=True)
    babies = models.ForeignKey(
        to='Babies',
        db_constraint=False,
        on_delete=models.CASCADE
    )
    hospital = models.CharField(max_length=60, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    class Meta:
        db_table = 'vaccinations'
        verbose_name = 'vaccinations information'
        verbose_name_plural = verbose_name
        ordering = ['-date']

    def __str__(self):
        return self.vaccinations

    @property
    def update_time(self):
        return self.updated_at.strftime("%Y-%m-%d %H:%M:%S")


class Medications(models.Model):
    name = models.CharField(max_length=255, blank=True, null=True)
    notes = models.TextField(blank=True, null=True)
    babies = models.ForeignKey(
        to='Babies',
        db_constraint=False,
        on_delete=models.CASCADE
    )
    hospital = models.CharField(max_length=60, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    class Meta:
        db_table = 'medications'
        verbose_name = 'medications information'
        verbose_name_plural = verbose_name
        ordering = ["id"]

    def __str__(self):
        return self.name

    @property
    def update_time(self):
        return self.updated_at.strftime("%Y-%m-%d %H:%M:%S")


class Contact(models.Model):
    name = models.CharField(max_length=50, blank=True, null=True)
    phone = models.CharField(max_length=50, blank=True, null=True)
    babies = models.ForeignKey(
        to='Babies',
        db_constraint=False,
        on_delete=models.CASCADE
    )
    hospital = models.CharField(max_length=60, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    class Meta:
        db_table = "contact"
        verbose_name = 'contact information'
        verbose_name_plural = verbose_name
        ordering = ["id"]

    def __str__(self):
        return self.name

    @property
    def update_time(self):
        return self.updated_at.strftime("%Y-%m-%d %H:%M:%S")


class Note(models.Model):
    note = models.TextField(blank=True, null=True)
    date = models.DateField(blank=True, null=True)
    babies = models.ForeignKey(
        to="Babies",
        db_constraint=False,
        on_delete=models.CASCADE
    )
    hospital = models.CharField(max_length=60, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    class Meta:
        db_table = "note"
        verbose_name = "note information"
        verbose_name_plural = verbose_name
        ordering = ["-date"]

    def __str__(self):
        return self.id

    @property
    def update_time(self):
        return self.updated_at.strftime("%Y-%m-%d %H:%M:%S")


class Allergies(models.Model):
    name = models.CharField(max_length=255, blank=True, null=True)
    notes = models.TextField(blank=True, null=True)
    babies = models.ForeignKey(
        to='Babies',
        db_constraint=False,
        on_delete=models.CASCADE
    )
    hospital = models.CharField(max_length=60, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    class Meta:
        db_table = "allergies"
        verbose_name = 'allergies information'
        verbose_name_plural = verbose_name
        ordering = ["id"]

    def __str__(self):
        return self.name

    @property
    def update_time(self):
        return self.updated_at.strftime("%Y-%m-%d %H:%M:%S")





